package com.fe.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fe.model.Deal;
import com.fe.model.Teacher;
import com.fe.service.DealService;
import com.fe.service.TeacherService;
import com.fe.util.JacksonData;
import com.fe.util.PageBean;
import com.fe.util.UploadFile;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

	@Autowired
	private TeacherService teacherService;
	@Autowired
	private DealService dealService;
	
	/**
	 * 
	 * 方法描述：根据条件查询家教
	 * @param teacher
	 * @param request
	 * @param map
	 * @param pages
	 * @return
	 * @author ssh
	 * @date 2018年8月25日 下午12:27:15
	 */
	@RequestMapping("/listTeacher")
	public String listTeacher(Teacher teacher, HttpServletRequest request, Map<String, Object> map, @RequestParam(required=false,defaultValue="1") int pages) {
		try {
			map = PageBean.serverMap(map, teacher, pages);
			teacherService.listTeacher(map, request);
			map = PageBean.map(map, pages, request);
		} catch (Exception e) {
			map.put("message", e.getMessage());
		}
		return "teacher/listTeacher";
	}
	
	/**
	 * 
	 * 方法描述：跳转到家教个人信息界面
	 * @return
	 * @author ssh
	 * @date 2018年8月21日 下午8:29:19
	 */
	@GetMapping("/getTeacher")
	public String getTeacher() {
		return "teacher/getTeacher";
	}
	
	/**
	 * 
	 * 方法描述：获取一个家教更新的数据，进行更新,如果是上传照片，则对图片进行随即命名，防止重名
	 * @param map
	 * @param teacher
	 * @return
	 * @author ssh
	 * @date 2018年8月21日 下午8:59:13
	 */
	@PostMapping("/getTeacher")
	public String updateTeacher(HttpServletRequest request, Map<String, Object> map, Teacher teacher, MultipartFile tea_photo_t) {
		//如果tea_photo_t不为空，则执行上传操作
		if (tea_photo_t != null) {
			try {
				String newName = UploadFile.upload(tea_photo_t);
				teacher.setTea_photo(newName);
				teacherService.update(request, map, teacher);
				map.put("message", "上传成功!");
			} catch (IllegalStateException | IOException e) {
				map.put("message", "上传失败!");
			}
		}else {
			teacherService.update(request, map, teacher);
		}
		return "teacher/getTeacher";
	}
	
	/**
	 * 
	 * 方法描述：家教修改账号时，判断账号是否已经被使用
	 * @param account
	 * @return
	 * @author ssh
	 * @date 2018年8月22日 下午8:43:58
	 */
	@PostMapping("/existAccount")
	@ResponseBody
	public JacksonData existAccount(String account) {
		JacksonData jd = new JacksonData();
		try {
			teacherService.exictAccount(account);
			jd.success();
		} catch (Exception e) {
			jd.failure(e.getMessage());
		}
		return jd;
	}
	
	/**
	 * 
	 * 方法描述：根据id查询一个家教的信息
	 * @param map
	 * @param tea_id
	 * @return
	 * @author ssh
	 * @date 2018年8月23日 上午10:59:16
	 */
	@RequestMapping("/loadTeacher/{tea_id}")
	public String loadTeacher(HttpServletRequest request,Map<String, Object> map,@PathVariable Integer tea_id,@RequestParam(required=false,defaultValue="1") int pages) {
		try {
			teacherService.loadTeacher(map, tea_id);
			PageBean.serverMap(map, new Deal(), pages);
			dealService.listDealByTeaId(map, tea_id);
			PageBean.map(map, pages, request);
			
		} catch (Exception e) {
			map.put("message", e.getMessage());
		}
		return "teacher/loadTeacher";
	}
	
	/**
	 * 
	 * 方法描述：跳转到学生证和身份证上传界面
	 * @return
	 * @author ssh
	 * @date 2018年8月25日 下午12:28:43
	 */
	@GetMapping("/uploadTeacher")
	public String uploadTeacher() {
		return "teacher/uploadTeacher";
	}
	
	/**
	 * 
	 * 方法描述：上传学生证或者身份证
	 * @param request
	 * @param map
	 * @param teacher
	 * @param tea_student_card_t
	 * @param tea_identity_card_t
	 * @return
	 * @author ssh
	 * @date 2018年8月25日 下午12:56:42
	 */
	@PostMapping("/uploadTeacher")
	public String uploadTeacher(HttpServletRequest request, Map<String, Object> map, Teacher teacher, MultipartFile tea_student_card_t, MultipartFile tea_identity_card_t) {
		//如果tea_student_card_t不为空，则执行上传学生证操作
		if (tea_student_card_t != null) {
			try {
				String newName = UploadFile.upload(tea_student_card_t);
				teacher.setTea_student_card(newName);
			} catch (IllegalStateException | IOException e) {
				map.put("message", "上传失败!");
			}
		}
		//如果tea_identity_card_t不为空，则执行上传身份证操作
		if (tea_identity_card_t != null) {
			try {
				String newName = UploadFile.upload(tea_identity_card_t);
				teacher.setTea_identity_card(newName);
			} catch (IllegalStateException | IOException e) {
				map.put("message", "上传失败!");
			}
		}
		//更新数据库中的记录
		try {
			teacherService.update(request, map, teacher);
			map.put("message", "上传成功!");
		} catch (Exception e) {
			map.put("message", "上传失败!");
		}
		return "teacher/uploadTeacher";
	}
	
	/**
	 * 
	 * 方法描述：改变路径跳转到个人信息
	 * @return
	 * @author ssh
	 * @date 2018年9月23日 上午11:23:01
	 */
	@GetMapping("/addMoney")
	public String addMoeny() {
		return "redirect:/teacher/getTeacher";
	}
	
	/**
	 * 
	 * 方法描述：充值
	 * @param request
	 * @param map
	 * @param patriarch
	 * @return
	 * @author ssh
	 * @date 2018年9月23日 上午11:23:22
	 */
	@PostMapping("/addMoney")
	public String addMoney(HttpServletRequest request, Map<String, Object> map, Teacher teacher) {
		try {
			HttpSession session = request.getSession();
			Teacher teacher2 = (Teacher) session.getAttribute("teacher");
			int balance = teacher2.getTea_balance();
			teacher.setTea_balance(teacher.getTea_balance()+balance);
			teacherService.update(request, map, teacher);
			map.put("message", "充值成功!");
		} catch (Exception e) {
			map.put("message", "充值失败!");
		}
		return "teacher/getTeacher";
	}
	
}
